package com.study.zzedu.fragment.home.onlinevideo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.study.zzedu.R;
import com.study.zzedu.adapter.OnLineLessonAdapter;
import com.study.zzedu.base.BaseFragment;
import com.study.zzedu.entity.OnLineLessonItem;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kiros on 2020/8/25.
 */

public class OnlineVideoFragment extends BaseFragment<OnlineVideoContract.IView, OnlineVideoPresenter> implements OnlineVideoContract.IView, View.OnClickListener {

    private RecyclerView rc_lesson;
    private RecyclerView.LayoutManager layoutManager;
    private OnLineLessonAdapter lessonAdapter;
    private List lessons = new ArrayList<>();
    int requestCount = 0;
    String lastLearnginTime = "";
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    protected OnlineVideoPresenter createPresenter() {
        return new OnlineVideoPresenter();
    }

    @Override
    protected OnlineVideoContract.IView createView() {
        return this;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_online_test;
    }

    @Override
    protected void initView(View view) {
        rc_lesson = view.findViewById(R.id.rc_lesson);
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        lessonAdapter = new OnLineLessonAdapter(getContext(), lessons);

        rc_lesson.setLayoutManager(layoutManager);
        rc_lesson.setAdapter(lessonAdapter);
    }

    @Override
    protected void initData() {
        requestCount = 0;
        lessons.clear();

        getPresenter().getDataList(getContext());
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    protected void initListener() { }

    @Override
    public void getData(String data) {
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void getLessonList(String data) {
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray yArray = jObject.getJSONArray("data");
            for (int y = 0; y < yArray.length(); y++) {
                lessons.add(
                        new OnLineLessonItem(
                                yArray.getJSONObject(y).getString("title"),
                                yArray.getJSONObject(y).getString("url"),
                                yArray.getJSONObject(y).getString("status"),
                                yArray.getJSONObject(y).getString("img"),
                                yArray.getJSONObject(y).getString("teacher"),
                                yArray.getJSONObject(y).getString("beginDate"),
                                yArray.getJSONObject(y).getString("endDate")));
            }
            if (!(yArray.length() > 0)) {
                //ToastUtil.showShortToast("在线直播无数据");
                return;
            }
            getActivity().findViewById(R.id.rl_default_online).setVisibility(View.GONE);
            lessonAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.toString();
        }
    }
    @Override
    public void getLessonProgress(String data) {

    }

}
